/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.datastore.genericdao.hibernate;

import java.util.Map;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

import com.googlecode.armycreator.datastore.genericdao.DaoQueryExecutor;
import com.googlecode.armycreator.datastore.genericdao.GenericDao;

/**
 * Hibernate generic DAO advisor. Dispatches GenericDao method calls to the advised
 * {@link com.googlecode.armycreator.datastore.genericdao.hibernate.HibernateGenericDao} and methods defined in
 * subinterfaces to {@link com.googlecode.armycreator.datastore.genericdao.DaoQueryExecutor}.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 */
public class HibernateDaoAdvisor implements IntroductionInterceptor {

    private final DaoQueryExecutor daoQueryExecutor;

    /**
     * Creates an instance of this class.
     * 
     * @param daoQueryExecutor
     *            The {@link DaoQueryExecutor} to be used to run query methods.
     */
    public HibernateDaoAdvisor(DaoQueryExecutor daoQueryExecutor) {
        this.daoQueryExecutor = daoQueryExecutor;
    }

    /**
     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
        final Class<?> daoClass = methodInvocation.getMethod().getDeclaringClass();
        Object result;

        if (daoClass.equals(GenericDao.class)) {
            result = methodInvocation.proceed();
        } else {
            Map parameters = (Map) methodInvocation.getArguments()[0];

            result = daoQueryExecutor
                    .executeQueryForMethod(new com.googlecode.armycreator.datastore.genericdao.QueryMethodInvocation(
                            methodInvocation.getMethod().getDeclaringClass().getName(), methodInvocation.getMethod()
                                    .getName(), parameters, methodInvocation.getMethod().getReturnType()));
        }

        return result;
    }

    /**
     * @see org.springframework.aop.DynamicIntroductionAdvice#implementsInterface(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean implementsInterface(Class intf) {
        return intf.isInterface() && GenericDao.class.isAssignableFrom(intf);
    }
}
